﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Customers;
using Shop.Web.AppCode;
using Action = Edge.Common.Web.Navigation.Action;

namespace Shop.Web
{
    public partial class CustomerDetails : ShopBasePage
    {

        [QueryParameter("action", IsRequired = false)]
        public Action action = Action.View;

        [QueryParameter("id", IsRequired = false)]
        public int id;
        
        protected void Page_Load(object sender, EventArgs e)
        {
           if (!IsPostBack)
           {
               PreDataBind();
           }

            gridCustomers.DataSource = Locator.GetService<ICustomerManager>().FindAll();
            gridCustomers.DataBind();
        }

        private void PreDataBind()
        {
            rblSex.DataSource = EnumUtils.GetEnumDataSource<SexType>();
            rblSex.DataTextField = "Key";
            rblSex.DataValueField = "Value";
            rblSex.DataBind();

            ddlRole.DataSource = EnumUtils.GetEnumDataSource<CustomerRole>();
            ddlRole.DataTextField = "Key";
            ddlRole.DataValueField = "Value";
            ddlRole.DataBind();
        }

        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            customer.FirstName = tbFirstName.Text;
            customer.LastName = tbLastName.Text;
            customer.Email = tbEmail.Text;
            customer.Passwrd = tbPasswrd.Text;
            customer.Sex = (SexType)Enum.Parse(typeof(SexType), rblSex.SelectedValue);
            customer.Birthday = DateTime.Parse(tbBirthday.Text);
            customer.Role = (CustomerRole)Enum.Parse(typeof(CustomerRole), ddlRole.SelectedValue);
            customer.Discount = Convert.ToInt32(tbDiscount.Text);
            customer.Phone = tbPhone.Text;
            customer.Note = tbNote.Text;
            Locator.GetService<ICustomerManager>().Save(customer);
        }
    }
}
